package top.mccat.service.impl;

import org.bukkit.Material;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import top.mccat.dao.StrengthDao;
import top.mccat.domain.StrengthExtra;
import top.mccat.domain.StrengthLevel;
import top.mccat.domain.StrengthLoreList;
import top.mccat.domain.StrengthStone;
import top.mccat.domain.config.EssentialsConfig;
import top.mccat.enums.Color;
import top.mccat.service.StrengthService;
import top.mccat.utils.ColorUtils;
import top.mccat.utils.ItemCheckerUtils;
import top.mccat.utils.ObjectParseUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Distance
 * @date 2022/5/13 17:47
 */
public class StrengthServiceImpl implements StrengthService {
    private List<String> strengthItem;
    private List<StrengthStone> strengthStones;
    private List<StrengthLevel> strengthLevels;
    private final StrengthDao strengthDao;
    private EssentialsConfig essentialsConfig;
    private StrengthExtra strengthExtra;
    public StrengthServiceImpl(List<String> strengthItem, List<StrengthStone> strengthStones, List<StrengthLevel> strengthLevels, StrengthDao strengthDao, EssentialsConfig essentialsConfig, StrengthExtra strengthExtra) {
        this.strengthItem = strengthItem;
        this.strengthStones = strengthStones;
        this.strengthLevels = strengthLevels;
        this.strengthDao = strengthDao;
        this.essentialsConfig = essentialsConfig;
        this.strengthExtra = strengthExtra;
    }

    @Override
    public ItemStack strengthItem(ItemStack stack) {
        int level = getLevel(stack);
        ItemMeta itemMeta = stack.getItemMeta();
        assert itemMeta != null;
        List<String> strengthLore = itemMeta.getLore();
        if(level == 0){
            itemMeta = strengthDao.strengthItem(stack, null, level);
        }else {
            itemMeta = strengthDao.strengthItem(stack, null, level);
        }
        stack.setItemMeta(itemMeta);
        return stack;
    }

    @Override
    public ItemStack haveStone(Inventory chestInventory) {
//        如果左右强化槽都不为空则可以正常强化
        ItemStack leftStrengthStone = chestInventory.getItem(13);
        if(leftStrengthStone == null){
            return null;
        }
        if(!ItemCheckerUtils.isAirMaterial(leftStrengthStone)){
            for (StrengthStone strengthStone : strengthStones){
                if(sameLoreChecker(leftStrengthStone, strengthStone.getLore())){
                    return leftStrengthStone;
                }
            }
        }
        ItemStack rightStrengthStone = chestInventory.getItem(14);
        if(rightStrengthStone == null){
            return null;
        }
        if(!ItemCheckerUtils.isAirMaterial(rightStrengthStone)){
            for (StrengthStone strengthStone : strengthStones){
                if(sameLoreChecker(rightStrengthStone, strengthStone.getLore())){
                    return leftStrengthStone;
                }
            }
        }
        return null;
    }

    @Override
    public ItemStack haveStone(PlayerInventory playerInventory) {
        return null;
    }

    @Override
    public boolean itemCanStrength(ItemStack stack){
        if(stack==null){
            return false;
        }
        if(!stack.hasItemMeta()){
            return false;
        }
        Material type = stack.getType();
        if(ItemCheckerUtils.isAirMaterial(stack)){
            return false;
        }
        for(String strType : strengthItem){
            if (type.toString().equals(strType)){
                return true;
            }
        }
        return false;
    }

    /**
     * 检测两个强化物品的lore是否相同
     * @param item 被检测物品
     * @param lore 物品lore
     * @return 是否为两个相同物品的lore
     */
    private boolean sameLoreChecker(@NotNull ItemStack item, List<String> lore){
        ItemMeta itemMeta = item.getItemMeta();
        if(ItemCheckerUtils.isAirMaterial(item)){
            return false;
        }
        assert itemMeta != null;
        if (!itemMeta.hasLore()){
            return false;
        }
        List<String> itemLore = itemMeta.getLore();
        return lore.equals(itemLore);
    }

    /**
     * 获取强化等级
     * @param stack 物品堆对象
     * @return 等级参数
     */
    private int getLevel(ItemStack stack){
        ItemMeta itemMeta = stack.getItemMeta();
        if(!stack.hasItemMeta()){
            return 0;
        }
        assert itemMeta != null;
        if (!itemMeta.hasLore()){
            return 0;
        }
        List<String> lore = itemMeta.getLore();
        if(lore==null){
            return 0;
        }
        assert lore.size()>2;
        String levelData = lore.get(2);
        return Integer.parseInt(levelData.substring(essentialsConfig.getTitle().length()));
    }

    private List<String> initStrengthLore(int level, ItemStack stack){
        List<String> strengthLore = new StrengthLoreList();
        strengthLore.add(essentialsConfig.getTitle());
        strengthLore.add(essentialsConfig.getDivider());
        strengthLore.addAll(getLevelStr(level));
        strengthLore.add(essentialsConfig.getDivider());
        //如果物品获取最大堆叠数为1，证明是攻击或防御性物品
        if(stack.getType().getMaxStackSize()==1){
            switch (stack.getType()){
                //防具统一lore
                //皮革套
                case LEATHER_HELMET:
                case LEATHER_CHESTPLATE:
                case LEATHER_LEGGINGS:
                case LEATHER_BOOTS:
                    //王八壳
                case TURTLE_HELMET:
                    //金套
                case GOLDEN_HELMET:
                case GOLDEN_CHESTPLATE:
                case GOLDEN_LEGGINGS:
                case GOLDEN_BOOTS:
                    //锁链套
                case CHAINMAIL_HELMET:
                case CHAINMAIL_CHESTPLATE:
                case CHAINMAIL_LEGGINGS:
                case CHAINMAIL_BOOTS:
                    //铁套
                case IRON_HELMET:
                case IRON_CHESTPLATE:
                case IRON_LEGGINGS:
                case IRON_BOOTS:
                    //钻石套
                case DIAMOND_HELMET:
                case DIAMOND_CHESTPLATE:
                case DIAMOND_LEGGINGS:
                case DIAMOND_BOOTS:
                    //下界合金套
                case NETHERITE_HELMET:
                case NETHERITE_CHESTPLATE:
                case NETHERITE_LEGGINGS:
                case NETHERITE_BOOTS:
                    initBaseArmorStrengthLore(level);
                    //默认为附加攻击伤害
                default:

                    break;
            }
        }else {
            //执行默认强化方法
            initBaseDamageStrengthLore(level);
        }
        return strengthLore;
    }

    /**
     * 生成基础物品伤害强化lore
     * @param level 等级参数
     * @return 属性String列表
     */
    private List<String> initBaseDamageStrengthLore(int level){
        List<String> strengthLore = new ArrayList<>();
        strengthLore.add("&b[&c伤害附加&b]&a==>&c"+strengthExtra.getSwordDamage()*level);
        return strengthLore;
    }

    /**
     * 生成基础护甲强化lore
     * @param level 强化等级
     * @return 属性String列表
     */
    private List<String> initBaseArmorStrengthLore(int level){
        List<String> strengthLore = new ArrayList<>();
        strengthLore.add("&b[&a伤害抵御&b]&a==>&b"+strengthExtra.getSwordDamage()*level);
        return strengthLore;
    }

    /**
     * 获取等级list参数
     * @param level 等级参数
     * @return 等级String列表
     */
    private List<String> getLevelStr(int level){
        int maxLevel = strengthLevels.size()-level;
        StringBuilder builder = new StringBuilder();
        List<String> levelList = new ArrayList<>();
        builder.append(ColorUtils.parseColorStr(Color.Cyan.getColorStr()));
        initLevelData(level, builder, levelList);
        builder.append(ColorUtils.parseColorStr(Color.Gray.getColorStr()));
        initLevelData(maxLevel, builder, levelList);
        return levelList;
    }

    /**
     * 进行等级参数生成
     * @param maxLevel 最大等级
     * @param builder StringBuilder
     * @param levelList 等级list
     */
    private void initLevelData(int maxLevel, StringBuilder builder, List<String> levelList) {
        for(int i = 0; i < maxLevel; i ++){
            builder.append(essentialsConfig.getLevelIcon());
            if(i%10 == 0 && i > 0){
                builder.delete(0,builder.length());
                levelList.add(builder.toString());
            }
        }
    }

    private void initStrengthExtra(ItemStack itemStack, int level, List<String> levelList){

    }

    public void setStrengthItem(List<String> strengthItem) {
        this.strengthItem = strengthItem;
    }

    public void setStrengthStone(List<StrengthStone> strengthStones) {
        this.strengthStones = strengthStones;
    }

    public void setEssentialsConfig(EssentialsConfig essentialsConfig) {
        this.essentialsConfig = essentialsConfig;
    }

    public void setStrengthLevels(List<StrengthLevel> strengthLevels) {
        this.strengthLevels = strengthLevels;
    }

    public void setStrengthStones(List<StrengthStone> strengthStones) {
        this.strengthStones = strengthStones;
    }
}
